package router

import (
	"SeatSystem/internal/logic"
	"SeatSystem/internal/model"
	"SeatSystem/internal/repository"
	"SeatSystem/internal/response"
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"strconv"
	"strings"
	"time"
)

var current model.User

func userVerify(c *gin.Context, id int) bool {
	if current.ID != id {
		c.JSON(http.StatusOK, response.Response{
			Status: false,
			Msg:    "账户信息错误，请重新登录",
		})
		return false
	}
	return true
}

func UserInfo(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("params[ID]"))
	if current.ID == id {
		c.JSON(http.StatusOK, response.UserResponse{
			Rsp:  response.Response{Status: true},
			User: current,
		})
	} else {
		c.JSON(http.StatusOK, response.UserResponse{
			Rsp: response.Response{
				Status: false,
				Msg:    "账号验证失效",
			},
		})
	}
}

// 登录
func Login(c *gin.Context) {
	phone := c.Query("params[phone]")
	pwd := c.Query("params[pwd]")
	user, err := repository.Login(phone, pwd)
	if err == nil {
		current = user
		c.JSON(http.StatusOK, response.UserResponse{
			Rsp:  response.Response{Status: true},
			User: current,
		})
	} else {
		log.Println("router.login", err)

		c.JSON(http.StatusOK, response.UserResponse{
			Rsp: response.Response{
				Status: false,
				Msg:    err.Error(),
			},
		})
	}

}

// 注册
func Register(c *gin.Context) {
	phone := c.Query("phone")
	pwd := c.Query("pwd")
	err := repository.Register(model.User{
		Pwd:    pwd,
		Phone:  phone,
		Type:   0,
		Points: 0,
	})
	if err != nil {
		log.Fatal("router.Register:", err)
	}
}

// 获取所有教室
func GetClassList(c *gin.Context) {
	classList, err := repository.GetClassList()
	if err != nil {
		log.Fatal("router.GetClassList", err)
	}
	c.JSON(http.StatusOK, response.ClassListResponse{
		Rsp:       response.Response{Status: response.YES},
		ClassList: classList,
	})
}

// todo 测试
// 获取教室座位表
func GetSeatList(c *gin.Context) {
	cid, _ := strconv.Atoi(c.Query("params[cid]"))
	length, _ := strconv.Atoi(c.Query("params[length]"))
	fmt.Println(cid, "-----", length)
	sTime := c.Query("params[startTime]")
	eTime := c.Query("params[endTime]")
	fmt.Println(sTime, "-----", eTime)

	temp, err := repository.GetSeatList(cid)
	if err != nil {
		log.Println("router.GetSeatList", err)
		log.Println("router.GetSeatList", cid)
	}
	seatList := logic.ParseRspSeatList(temp)
	if sTime != "" && eTime != "" {
		sTime = strings.Replace(sTime[:len(sTime)-1], "T", " ", 1)
		eTime = strings.Replace(eTime[:len(eTime)-1], "T", " ", 1)
		t1, _ := time.Parse("2006-01-02 15:04:05", sTime)
		t2, _ := time.Parse("2006-01-02 15:04:05", eTime)
		seatList = logic.ScreeningSeatTime(seatList, t1, t2)
	}
	c.JSON(http.StatusOK, response.SeatListResponse{
		Rsp:      response.Response{Status: response.YES},
		SeatList: logic.SortSeat(length, seatList),
	})
}

// todo 测试
// 选择座位（时间）
func ChooseSeat(c *gin.Context) {
	sid, _ := strconv.Atoi(c.Query("params[sid]"))
	uid, _ := strconv.Atoi(c.Query("params[uid]"))
	sTime := c.Query("params[startTime]")
	eTime := c.Query("params[endTime]")
	if !userVerify(c, uid) {
		return
	}
	if sTime == "" || eTime == "" || sid == 0 {
		c.JSON(http.StatusOK, response.Response{Status: response.NO, Msg: "数据错误"})
		return
	}
	sTime = strings.Replace(sTime[:len(sTime)-1], "T", " ", 1)
	eTime = strings.Replace(eTime[:len(eTime)-1], "T", " ", 1)
	fmt.Println(eTime)
	fmt.Println(sTime)
	t1, _ := time.Parse("2006-01-02 15:04:05", sTime)
	t2, _ := time.Parse("2006-01-02 15:04:05", eTime)
	fmt.Println(t1)
	fmt.Println(t2)
	repository.SaveTime(model.Time{
		SID:       sid,
		StartTime: t1,
		EndTime:   t2,
	})
	repository.SaveHistroy(model.History{
		UID:       current.ID,
		CID:       sid,
		StartTime: t1,
		EndTime:   t2,
	})
	c.JSON(http.StatusOK, response.Response{Status: response.YES})
}

// 获取历史记录
func GetHistory(c *gin.Context) {
	list, err := repository.GetHistoryList(current.ID)
	if err != nil {
		log.Println("router.GetHistory", err)
		return
	}
	c.JSON(http.StatusOK, response.HistoryListResponse{
		Rsp:         response.Response{Status: response.YES},
		HistoryList: list,
	})
}

func FeedBack(c *gin.Context) {
	content := c.Query("params[content]")
	repository.SaveFeedBack(model.FeedBack{
		Content: content,
	})
	c.JSON(http.StatusOK, response.Response{
		Status: true,
	})
}

func GetFeedBack(c *gin.Context) {
	c.JSON(http.StatusOK, response.FeedBackResponse{
		Rsp:          response.Response{Status: true},
		FeedBackList: repository.GetFeedBackList(),
	})
}
